//package com.framework.service.config.initRedisConfig;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.cache.Cache;
//import org.springframework.cache.annotation.CachingConfigurerSupport;
//import org.springframework.cache.interceptor.CacheErrorHandler;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.cache.RedisCacheConfiguration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializationContext;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
//import java.time.Duration;
//
///**
// * @author 龘鵺
// * @className com.framework.web.config.initRedisConfig
// * @description springsession和redis单机或集群初始化类, 因为使用了这个类初始化，所以吧RedisConfig注释掉
// * @datetime 2019-01-09 11:11:11
// * @version 1.0
// */
////@EnableRedisHttpSession
////@Slf4j
//@Configuration
//public class RedisConfig extends CachingConfigurerSupport {
//    private Logger log = LoggerFactory.getLogger(RedisConfig.class);
//    @Autowired
//    private RedisTemplate redisTemplate;
//    @Value("${spring.redis.entityttl}")
//    private Integer entityttl;//存储时间
//
//    /**
//     * @param redisConnectionFactory 1 redis工厂参数对象
//     * @return org.springframework.data.redis.cache.RedisCacheManager
//     * @title 设置cache缓存工厂存在时间，单位分
//     * @description 设置cache缓存工厂存在时间，单位分
//     * @author 龘鵺
//     * @datetime 2020-01-09 11:11:11
//     */
//    @Bean
//    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
////        ObjectMapper om = new ObjectMapper();
////        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
////        om.activateDefaultTyping(new ObjectMapper().getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
////        jackson2JsonRedisSerializer.setObjectMapper(om);
//
//        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
//                .entryTtl(Duration.ofMinutes(entityttl))
//                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
//                //禁用缓存空值
//                .disableCachingNullValues();
//
//        return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(cacheConfiguration).transactionAware().build();
//    }
//
//    /**
//     * @return org.springframework.data.redis.serializer.RedisSerializer<java.lang.String>
//     * @title 设置redis缓存键格式
//     * @description 设置redis缓存键格式
//     * @author 龘鵺
//     * @datetime 2020-01-09 11:11:11
//     */
//    private RedisSerializer<String> keySerializer() {
//        return new StringRedisSerializer();
////        return new Jackson2JsonRedisSerializer(Object.class);
//    }
//
//    /**
//     * @return org.springframework.data.redis.serializer.RedisSerializer<java.lang.Object>
//     * @title 设置redis缓存值格式
//     * @description 设置redis缓存值格式
//     * @author 龘鵺
//     * @datetime 2020-01-09 11:11:11
//     */
//    private RedisSerializer<Object> valueSerializer() {
//        return new GenericJackson2JsonRedisSerializer();
////        return new JdkSerializationRedisSerializer(getClass().getClassLoader());
//    }
//
//
//    /**
//     * @return org.springframework.data.redis.core.RedisTemplate
//     * @title redis初始化设置, 解决redis插入中文乱码
//     * @description redis初始化设置, 解决redis插入中文乱码
//     * @author 龘鵺
//     * @datetime 2019-01-09 11:11:11
//     */
//    @Bean
//    public RedisTemplate redisTemplateInit() {
////        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
////        ObjectMapper om = new ObjectMapper();
////        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
////        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
////        jackson2JsonRedisSerializer.setObjectMapper(om);
//
////        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
////        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//
//
//        //设置序列化Key的实例化对象
//        redisTemplate.setKeySerializer(keySerializer());
//        redisTemplate.setHashKeySerializer(keySerializer());
//        //设置序列化Value的实例化对象
//        redisTemplate.setValueSerializer(valueSerializer());
//        redisTemplate.setHashValueSerializer(valueSerializer());
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
//
//    /**
//     * @param redisConnectionFactory 1 redis链接工厂
//     * @return org.springframework.data.redis.core.RedisTemplate
//     * @title redis初始化工厂设置
//     * @description redis初始化工厂设置
//     * @author 龘鵺
//     * @datetime 2019-01-09 11:11:11
//     */
//    @Bean
//    @ConditionalOnMissingBean(RedisTemplate.class)
//    public RedisTemplate<Object, Object> redisTemplate(
//            RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
//        template.setConnectionFactory(redisConnectionFactory);
//        return template;
//    }
//
//    /**
//     * @param redisConnectionFactory 1 redis链接工厂
//     * @return org.springframework.data.redis.core.StringRedisTemplate
//     * @title redis初始化工厂设置
//     * @description redis初始化工厂设置
//     * @author 龘鵺
//     * @datetime 2019-01-09 11:11:11
//     */
//    @Bean
//    @ConditionalOnMissingBean(StringRedisTemplate.class)
//    public StringRedisTemplate stringRedisTemplate(
//            RedisConnectionFactory redisConnectionFactory) {
//        StringRedisTemplate template = new StringRedisTemplate();
//        template.setConnectionFactory(redisConnectionFactory);
//        return template;
//    }
//
//    @Override
//    public CacheErrorHandler errorHandler() {
//        CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() {
//            @Override
//            public void handleCacheGetError(RuntimeException e, Cache cache, Object key) {
//                log.warn("Redis occur handleCacheGetError：key: [{}]", key);
//            }
//
//            @Override
//            public void handleCachePutError(RuntimeException e, Cache cache, Object key, Object value) {
//                log.warn("Redis occur handleCachePutError：key: [{}]；value: [{}]", key, value);
//            }
//
//            @Override
//            public void handleCacheEvictError(RuntimeException e, Cache cache, Object key) {
//                log.warn("Redis occur handleCacheEvictError：key: [{}]", key);
//            }
//
//            @Override
//            public void handleCacheClearError(RuntimeException e, Cache cache) {
//                log.warn("Redis occur handleCacheClearError");
//            }
//        };
//        return cacheErrorHandler;
//    }
//}
